package com.snack.system.controller;

import com.snack.common.core.constant.UserConstants;
import com.snack.common.core.web.controller.BaseController;
import com.snack.common.core.web.domain.AjaxResult;
import com.snack.common.core.web.page.TableDataInfo;
import com.snack.common.log.annotation.Log;
import com.snack.common.log.enums.BusinessType;
import com.snack.common.security.annotation.RequiresPermissions;
import com.snack.common.security.utils.SecurityUtils;
import com.snack.system.domain.TsGoods;
import com.snack.system.service.ITsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 * 
 * @author snack
 * @date 2022-09-18
 */
@RestController
@RequestMapping("/goods")
public class TsGoodsController extends BaseController
{
    @Autowired
    private ITsGoodsService goodsService;

    /**
     * 查询商品列表
     */
    @RequiresPermissions("system:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(TsGoods goods)
    {
        startPage();
        List<TsGoods> list = goodsService.selectList(goods);
        return getDataTable(list);
    }


    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("system:goods:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(goodsService.selectById(goodsId));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("system:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TsGoods goods)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkNameUnique(goods)))
        {
            return AjaxResult.error("新增商品'" + goods.getGoodsName() + "'失败，商品在同类型下名称已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkCodeUnique(goods)))
        {
            return AjaxResult.error("新增商品'" + goods.getGoodsName() + "'失败，商品编码已存在");
        }
        goods.setCreateBy(SecurityUtils.getUsername());
        return toAjax(goodsService.insert(goods));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TsGoods goods)
    {
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkNameUnique(goods)))
        {
            return AjaxResult.error("修改商品'" + goods.getGoodsName() + "'失败，商品在同类型下名称已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(goodsService.checkCodeUnique(goods)))
        {
            return AjaxResult.error("修改商品'" + goods.getGoodsName() + "'失败，商品编码已存在");
        }
        goods.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(goodsService.update(goods));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("system:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TsGoods goods)
    {
        goods.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(goodsService.updateStatus(goods));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsService.deleteByIds(goodsIds));
    }
}
